package id.co.shuba.spamobile.widget;

import id.co.shuba.spamobile.R;

import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class GroupAdapter extends BaseAdapter {
	private ArrayList<IListItem> menuList;
	private Activity activity;

	public GroupAdapter(Activity activity, ArrayList<IListItem> menuList) {
		super();
		this.menuList = menuList;
		this.activity = activity;
	}

	public int getCount() {
		return menuList.size();
	}

	public String getItem(int position) {
		return null;
	}

	public IListItem getMenu(int position) {
		return menuList.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

	public static class ViewHolder {
		public TextView title;
		public TextView value;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflator = activity.getLayoutInflater();
		IListItem menu = menuList.get(position);		
		
		if (menu instanceof ListItem) {
			ListItem item = (ListItem) menu;
			
			ViewHolder view = null;			
			
			if (convertView != null && convertView.getTag() instanceof ViewHolder) {				
				view = (ViewHolder) convertView.getTag();
			} else {
				view = new ViewHolder();

				convertView = inflator.inflate(R.layout.list_item, null);

				view.title = (TextView) convertView.findViewById(R.id.txtTitle);
				view.value = (TextView) convertView.findViewById(R.id.txtValue);

				convertView.setTag(view);
			}

			view.title.setText(item.getTitle());
			view.value.setText(item.getValue());
		} else {
			GroupInfo item = (GroupInfo) menu;
			TextView group;
			
			if (convertView != null && convertView.getTag() instanceof TextView) {
				group = (TextView) convertView.getTag();
			} else {				
				convertView = inflator.inflate(R.layout.group_item, null);
				group = (TextView) convertView.findViewById(R.id.txtGroup);
				convertView.setTag(group);
			}

			group.setText(item.getHeader());
		}

		return convertView;
	}
}
